package com.nox.trademe.testapp.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nox.trademe.testapp.R;
import com.nox.trademe.testapp.models.Subcategory;

import java.util.ArrayList;

public class CustomExpandableAdapter extends BaseExpandableListAdapter {

    private ArrayList<Subcategory> subcategories;
    private int groupLayoutID;
    private int itemLayoutID;
    private Context context;

    public CustomExpandableAdapter(ArrayList<Subcategory> subs, Context context) {
        this.subcategories = subs;
        this.groupLayoutID = R.layout.group_layout;
        this.itemLayoutID = R.layout.item_layout;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return subcategories.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return subcategories.get(groupPosition).getSubcategories().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return subcategories.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return ((Object)subcategories.get(groupPosition)).hashCode();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return subcategories.get(groupPosition).getSubcategories().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return ((Object)subcategories.get(groupPosition).getSubcategories().get(childPosition)).hashCode();
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.group_layout, parent, false);
        }

        ImageView indicator = (ImageView)view.findViewById( R.id.explicit_indicator);
        if( indicator != null ) {
            if( getChildrenCount( groupPosition ) != 0 ) {
                indicator.setVisibility( View.VISIBLE );
                if(isExpanded) {
                    indicator.setImageResource(R.drawable.ic_action_expand);
                }
                else {
                    indicator.setImageResource(R.drawable.ic_action_collapse);
                }
            }
        }

        TextView name = (TextView) view.findViewById(R.id.categoryName);
        TextView count = (TextView) view.findViewById(R.id.categoryCount);

        Subcategory current = subcategories.get(groupPosition);

        name.setText(current.getName());

        count.setText("");

        if (getChildrenCount(groupPosition) == 0) {
            count.setText(current.getCount() + " " + context.getString(R.string.count_phrase));
            if( indicator != null ) {
                indicator.setVisibility(View.INVISIBLE);
            }
        }

        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_layout, parent, false);
        }

        TextView subName = (TextView) view.findViewById(R.id.subcategoryName);
        TextView subCount = (TextView) view.findViewById(R.id.subcategoryCount);

        Subcategory subcategory = subcategories.get(groupPosition).getSubcategories().get(childPosition);

        subName.setText(subcategory.getName());
        subCount.setText(subcategory.getCount() + " " + context.getString(R.string.count_phrase));

        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
